#define NearHouse(%0,%1) IsPlayerInRangeOfPoint(%0,3,HouseInfo[%1][PickupX],HouseInfo[%1][PickupY],HouseInfo[%1][PickupZ])
#define MaxHouses 50

new CarShopCars[24][2]=
{
	{602, 6000},
	{429, 6500},
	{496, 4000},
	{402, 5800},
	{541, 6800},
	{415, 6000},
	{589, 4500},
	{587, 5200},
	{565, 4500},
	{411, 7000},
	{559, 6800},
	{603, 4800},
	{475, 4500},
	{506, 6000},
	{451, 6000},
	{558, 4000},
	{477, 5800},
	{581, 2500},
	{522, 3200},
	{461, 2500},
	{521, 2500},
	{463, 3000},
	{468, 3000},
	{471, 4000}
},
DisplayCar[MAX_PLAYERS],SelectedCar[MAX_PLAYERS];

enum houseinfo
{
	HouseName[60],	//SveNme
	HouseLabel[60],	//SveNme
	HouseCost,		//Cost
	HouseOwner[24],	//Owner
	HouseLocked,    //Locked
	Float:PickupX, 	//PicLoc
 	Float:PickupY, 	//PicLoc
	Float:PickupZ, 	//PicLoc
	Float:TeleX, 	//IntLoc
	Float:TeleY, 	//IntLoc
	Float:TeleZ, 	//IntLoc
	Interior, 		//IntID
	HouseSell, 		//SelPrc
	Owned, 			//Owned
	Virtual,		//VitWld
	Float:GarageX, 	//GrgLoc
	Float:GarageY, 	//GrgLoc
	Float:GarageZ 	//GrgLoc
}
enum carinfo
{
	c_model,
	Float:pos_x,
	Float:pos_y,
	Float:pos_z,
	Float:rot_z,
	colour1,
	colour2,
	mods[14]
}
new
	HouseInfo[MaxHouses][houseinfo],
	CarInfo[MaxHouses][carinfo],
	HouseCount = -1,
	HousePickup[MaxHouses],
	InHouse[MAX_PLAYERS] = -1,
	HouseCar[MaxHouses],
	HouseInteriorData[][4]=
	{
		{2319, 	-1023,	1050, 9},
		{234, 	1187,	1080, 3},
		{225, 	1240, 	1082, 2},
		{239, 	1114, 	1080, 5},
		{446, 	1397, 	1084, 2},
		{24, 	1341, 	1084, 10},
		{-262, 	1456, 	1084, 4},
		{22, 	1404, 	1084, 5},
		{140, 	1367, 	1083, 5},
		{234, 	1065, 	1084, 6},
		{-285, 	1471, 	1084, 15},
		{2365, 	-1133,	1050, 8},
		{84, 	1324,	1083, 9}
	};
AddHouse(Housename[], Filename[], Cost, Float:Pickupx, Float:Pickupy, Float:Pickupz, interior, sell, Float:Gx = 0.0, Float:Gy = 0.0, Float:Gz = 0.0)
{
	HouseCount ++;
	new ID = HouseCount;
	format(HouseInfo[ID][HouseName], 60, "%s", Filename);
	format(HouseInfo[ID][HouseLabel], 60, "%s", Housename);
	HouseInfo[ID][HouseCost] = Cost;
	HouseInfo[ID][PickupX] = Pickupx;
	HouseInfo[ID][PickupY] = Pickupy;
	HouseInfo[ID][PickupZ] = Pickupz;
	HouseInfo[ID][TeleX] = HouseInteriorData[interior][0];
	HouseInfo[ID][TeleY] = HouseInteriorData[interior][1];
	HouseInfo[ID][TeleZ] = HouseInteriorData[interior][2];
	HouseInfo[ID][Virtual] = 2000000 + ID;
	HouseInfo[ID][Interior] = HouseInteriorData[interior][3];
	HouseInfo[ID][HouseSell] = sell;
	HouseInfo[ID][GarageX] = Gx;
	HouseInfo[ID][GarageY] = Gy;
	HouseInfo[ID][GarageZ] = Gz;
	format(HouseInfo[ID][HouseOwner], 24, "None");
	file_Open(hFile);
	if(file_IsKey(Filename))
	{
		new line[128], n[128];
		format(line, 128, file_GetStr(Filename));
		sscanf(line, "p<,>s[128]e<dffffdddddddddddddddd>", n, CarInfo[ID]);
		format(HouseInfo[ID][HouseOwner], 24, "%s", n);
		HouseInfo[ID][Owned]=1;
		if(CarInfo[ID][c_model]>=400&&CarInfo[ID][c_model]<=612)HouseCar[ID]=CreateVehicle(CarInfo[ID][c_model], CarInfo[ID][pos_x], CarInfo[ID][pos_y], CarInfo[ID][pos_z], CarInfo[ID][rot_z], CarInfo[ID][colour1], CarInfo[ID][colour2], (30*60));
		for(new comp;comp<14;comp++)AddVehicleComponent(HouseCar[ID], CarInfo[ID][mods][comp]);
	}
	file_Close();
	HousePickup[ID] = CreateDynamicPickup(1273, 23, Pickupx, Pickupy, Pickupz);
	CreateDynamic3DTextLabel(Housename, LGREEN, Pickupx, Pickupy, Pickupz+0.75, 30.0);
	
	
//	AddNewHouse("Sunny Drive", ID+1, 5, 14000, Pickupx, Pickupy, Pickupz);
}
script_Buyable_Pickup(playerid, pickupid)
{
	for(new J;J<MaxHouses;J++)
	{
		if(pickupid == HousePickup[J])
		{
			new str[75];
			if(HouseInfo[J][Owned] == 1)
			{
				if(strcmp(HouseInfo[J][HouseOwner], GetName(playerid)))format(str, sizeof(str), "~b~%s~n~~r~House Info~n~~g~Owner: ~w~%s", HouseInfo[J][HouseLabel], GameTextStr(HouseInfo[J][HouseOwner]));
				else format(str, sizeof(str), "~b~%s~n~~r~Type /enter to go in", HouseInfo[J][HouseLabel]);
			}
			if(HouseInfo[J][Owned] == 0)
			{
				format(str, sizeof(str), "~b~%s~n~~r~House Info~n~~g~Owner: ~w~Nobody", HouseInfo[J][HouseLabel]);
				msg(playerid, 0x67F6F6AA, "This house is up for sale! Type /buy to buy it");
			}
			new str2[100];
			format(str2, sizeof(str2), "~g~Cost price:~w~ %d ~n~~g~Sell Price:~w~ %d", HouseInfo[J][HouseCost], HouseInfo[J][HouseSell]);
			new str3[150];
			format(str3, sizeof(str3), "%s~n~%s", str, str2);
			GameTextForPlayer(playerid, str3, 3500, 3);
		}
	}
	if(pickupid == CarShop)
	{
	    ShowCarShopMenu(playerid);
	}
}
ShowCarShopMenu(playerid, car=0)
{
	new str[300];
	str="Exit Car Shop";
	for(new x;x<(sizeof(CarShopCars));x++)format(str, 300, "%s\n$%d\t%s", str, CarShopCars[x][1], VehicleNames[CarShopCars[x][0]-400]);
	ShowPlayerDialog(playerid, d_CarBuy, DIALOG_STYLE_LIST, "Choose a car to buy", str, "Veiw", "Buy");

	DisplayCar[playerid]=CreateVehicle(CarShopCars[car][0], -1655.544433, 1208.156127, 21.0, 242.591857, -1, -1, 1000);
	SetVehicleVirtualWorld(DisplayCar[playerid], 4);
	SetPlayerVirtualWorld(playerid, 4);
	SetPlayerCameraPos(playerid, -1649.428955, 1213.219970, 23.0);
	SetPlayerCameraLookAt(playerid, -1655.544433, 1208.156127, 21.0);
	SetPlayerPos(playerid, -1649.428955, 1213.219970, 21.0);
}
script_Buyable_OnEnterExitMod(playerid, enterexit)
{
	if(!enterexit)
	for(new h;h<MaxHouses;h++)
	if(GetPlayerVehicleID(playerid)==HouseCar[h])SaveHouseCar(playerid, h, 0);
}
script_Buyable_OnExitVeh(playerid, vehicleid)
{
	for(new ID; ID<MaxHouses; ID++)
	{
		if(IsPlayerInRangeOfPoint(playerid, 2.0, HouseInfo[ID][GarageX], HouseInfo[ID][GarageY], HouseInfo[ID][GarageZ]))
		{
		    if(!strcmp(HouseInfo[ID][HouseOwner], GetName(playerid)))
		    {
		        if(vehicleid==HouseCar[ID])SaveHouseCar(playerid, ID, 1);
		        else msg(playerid, RED, "You can only save a car you have bought!");
				return 1;
			}
			else msg(playerid, RED, "This house does not belong to you");
		}
	}
	return 1;
}
SaveHouseCar(playerid, ID, pos)
{
	new
	    vehicleid=GetPlayerVehicleID(playerid),
		str[130],
		tmp[14];
	if(pos)
	{
		GetVehiclePos(vehicleid, CarInfo[ID][pos_x], CarInfo[ID][pos_y], CarInfo[ID][pos_z]);
		GetVehicleZAngle(vehicleid, CarInfo[ID][rot_z]);
	}
	for(new comp;comp<14;comp++)CarInfo[ID][mods][comp]=GetVehicleComponentInSlot(vehicleid, comp);
	for(new comp;comp<14;comp++)tmp[comp]=CarInfo[ID][mods][comp];
	format(str, 130, "%s,%d,%f,%f,%f,%f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", GetName(playerid), CarInfo[ID][c_model], CarInfo[ID][pos_x], CarInfo[ID][pos_y], CarInfo[ID][pos_z], CarInfo[ID][rot_z], CarInfo[ID][colour1], CarInfo[ID][colour2], tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13]);

	file_Open(hFile);
	file_SetStr(HouseInfo[ID][HouseName], str);
	file_Save(hFile);
	file_Close();
	GameTextForPlayer(playerid, "Vehicle Saved", 2000, 5);
}
script_Houses_OnEnterVeh(playerid, vehicleid)
{
	new Float:pp[3];
	GetPlayerPos(playerid, pp[0], pp[1], pp[2]);
	for(new h; h<MaxHouses; h++)
	{
		if(vehicleid == HouseCar[h])
		{
			if(strcmp(HouseInfo[h][HouseOwner], GetName(playerid)))
			{
				SetPlayerPos(playerid, pp[0], pp[1], pp[2]);
				msg(playerid, RED, "This car does not belong to you");
			}
		}
	}
}

CMD:buy(playerid, params[])
{
		for(new i; i<MaxHouses; i++)
		{
			if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[i][PickupX], HouseInfo[i][PickupY], HouseInfo[i][PickupZ]))
			{
			    new result = BuyHouse(playerid, i);
			    if(result==0xDEADBEEF)msgF(playerid, RED, "You don't have enough money to buy this house, you need an extra $%d", HouseInfo[i][HouseCost]-GetPlayerMoney(playerid));
			    if(result==0xDEADBABE)msg(playerid, RED, "This house is already owned!");
			    if(result==0xB000B1E5)msg(playerid, GREEN, "House Purchased!");
				return 1;
			}
		}
		msg(playerid, RED, "You are not at a house");
		return 1;
}
CMD:sell(playerid, params[])
{
		for(new i; i<MaxHouses; i++)
		{
			if(NearHouse(playerid, i))
			{
			    new result = SellHouse(playerid, i);
			    if(result==0xFF)msg(playerid, RED, "You don't own this house!");
				if(result==0xAA)msg(playerid, GREEN, "House Sold!");
				return 1;
			}
		}
		msg(playerid, RED, "You are not at a house");
		return 1;
}
CMD:enter(playerid, params[])
{
		for(new i; i<MaxHouses; i++)
		{
			if(IsPlayerInRangeOfPoint(playerid, 3, HouseInfo[i][PickupX], HouseInfo[i][PickupY], HouseInfo[i][PickupZ]))
			{
			    new result = EnterHouse(playerid, i);
			    if(result==0xAA)msg(playerid, GREEN, "You have entered your house, use /lockhouse to let other players in or keep them out");
			    if(result==0xAB)msg(playerid, BLUE, "The owner is home, you knock");
			    if(result==0xAC)msg(playerid, RED, "The owner isn't home");
			    if(result==0xAD)msgF(playerid, GREEN, "You have entered %s's house", HouseInfo[i][HouseOwner]);
			    if(result==0xFF)msg(playerid, RED, "ERROR: 0xFF");
			}
		}
		msg(playerid, RED, "You are not at a house");
}
CMD:exit(playerid, params[])
{
		if(InHouse[playerid] == -1) return msg(playerid, RED, "You are not in a house");
		else
		{
			new result = ExitHouse(playerid);
			if(result==0xAA)msg(playerid, GREEN, "You have exited your house, it is now locked");
		}
		return 1;
}
CMD:lockhouse(playerid, params[])
{
		for(new i; i<MaxHouses; i++)
		{
	    	if(InHouse[playerid]==i && !strcmp(GetName(playerid), HouseInfo[i][HouseOwner]))
			{
				new result = SetHouseLock(i);
				if(result==0)msg(playerid, YELLOW, "Your House is now locked");
				if(result==1)msg(playerid, YELLOW, "Your House is now unlocked");
	    	}
	    }
	    return 1;
}


BuyHouse(playerid, houseid)
{
	if(GetPlayerMoney(playerid) < HouseInfo[houseid][HouseCost])return 0xDEADBEEF;
	if(HouseInfo[houseid][Owned] == 1)return 0xDEADBABE;
	GivePlayerMoney(playerid, - HouseInfo[houseid][HouseCost]);
	HouseInfo[houseid][Owned] = 1;
	strcat(HouseInfo[houseid][HouseOwner], GetName(playerid), 24);
	new str[50];
	format(str, 50, "%s,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0", GetName(playerid));
	file_Open(hFile);
	file_SetStr(HouseInfo[houseid][HouseName], str);
	file_Save(hFile);
	file_Close();
	return 0xB000B1E5;
}
SellHouse(playerid, houseid)
{
	if(strcmp(GetName(playerid), HouseInfo[houseid][HouseOwner])) return 0xFF;
	GivePlayerMoney(playerid, HouseInfo[houseid][HouseSell]);
	HouseInfo[houseid][Owned] = 0;
	HouseInfo[houseid][HouseOwner][0]='\0';
	file_Open(hFile);
	file_SetStr(HouseInfo[houseid][HouseName], "SOLD");
	file_Save(hFile);
	file_Close();
	return 0xAA;
}
EnterHouse(playerid, houseid)
{
	if(!strcmp(GetName(playerid), HouseInfo[houseid][HouseOwner]))
	{
		SetPlayerPos(playerid, HouseInfo[houseid][TeleX], HouseInfo[houseid][TeleY], HouseInfo[houseid][TeleZ]);
		SetPlayerInterior(playerid, HouseInfo[houseid][Interior]);
		SetPlayerVirtualWorld(playerid, HouseInfo[houseid][Virtual]);
		InHouse[playerid] = houseid;
		return 0xAA;
	}
	else
	{
		if(HouseInfo[houseid][HouseLocked])
		{
		    new pid=0xFF;
			PlayerLoop(a)if(!strcmp(GetName(a), HouseInfo[houseid][HouseOwner]))if(InHouse[a]==houseid)pid=a;
			if(pid!=0xFF)
			{
				msg(pid, YELLOW, "someone is at your door");
				return 0xAB;
			}
			else return 0xAC;
		}
		else
		{
			SetPlayerPos(playerid, HouseInfo[houseid][TeleX], HouseInfo[houseid][TeleY], HouseInfo[houseid][TeleZ]);
			SetPlayerInterior(playerid, HouseInfo[houseid][Interior]);
			SetPlayerVirtualWorld(playerid, HouseInfo[houseid][Virtual]);
			InHouse[playerid] = houseid;
			return 0xAD;
		}
	}
}
ExitHouse(playerid)
{
	new i = InHouse[playerid];
	InHouse[playerid] = -1;
	SetPlayerPos(playerid, HouseInfo[i][PickupX], HouseInfo[i][PickupY], HouseInfo[i][PickupZ]);
	SetPlayerInterior(playerid, 0);
	SetPlayerVirtualWorld(playerid, 0);
	if(!strcmp(GetName(playerid), HouseInfo[i][HouseOwner]))
	{
		HouseInfo[i][HouseLocked]=1;
		return 0xAA;
	}
	return 0xFF;
}
SetHouseLock(houseid)
{
	HouseInfo[houseid][HouseLocked]=switchbool(HouseInfo[houseid][HouseLocked]);
	return HouseInfo[houseid][HouseLocked];
}


script_Buyable_DialogResponse(playerid, dialogid, response, listitem)
{
	if(dialogid == d_CarBuy)
	{
	    if(listitem==0)
	    {
			DestroyVehicle(DisplayCar[playerid]);
			SetPlayerVirtualWorld(playerid, 0);
			SetCameraBehindPlayer(playerid);
	    }
	    if(response)
	    {
			DestroyVehicle(DisplayCar[playerid]);
			ShowCarShopMenu(playerid, listitem-1);
	    }
	    else
		{
		    if(GetPlayerMoney(playerid) < CarShopCars[listitem-1][1])
		    {
		        ShowCarShopMenu(playerid);
		        msg(playerid, RED, "You don't have enough money");
		    }
		    else
		    {
		    	new str[100];
		    	format(str, 100, "Click Buy to buy this car worth $%d or click Back to choose another\n{FF0101}Warning: Buying a new car will erase any previously saved car at your house", CarShopCars[listitem-1][1]);
				ShowPlayerDialog(playerid, d_CarBuyConfirm, DIALOG_STYLE_MSGBOX, "Are you sure you want to buy this car?", str, "Back", "Buy");
				SelectedCar[playerid]=listitem-1;
			}
		}
	}
	if(dialogid == d_CarBuyConfirm)
	{
	    if(!response)
		{
			for(new h; h<MaxHouses; h++)
			{
				if(!strcmp(HouseInfo[h][HouseOwner], GetName(playerid)))
				{
					DestroyVehicle(DisplayCar[playerid]);
					CreateVehicle(SelectedCar[playerid], -1655.544433, 1208.156127, 21.0, 242.591857, -1, -1, 10000);
					SelectedCar[playerid]=-1;
					GivePlayerMoney(playerid, -CarShopCars[SelectedCar[playerid]][1]);
					break;
				}
				else
				{
					DestroyVehicle(DisplayCar[playerid]);
					SetPlayerVirtualWorld(playerid, 0);
					SetCameraBehindPlayer(playerid);
					msg(playerid, RED, "You do not own a house, you need to buy a house to save your car at");
					break;
				}
			}
		}
		else ShowCarShopMenu(playerid);
	}
	return 1;
}
